home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / accessibility / AccessibleBundle.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  3.9 KB  |  116 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)AccessibleBundle.java    1.9 98/08/26
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package javax.accessibility;
  16.  
  17. import java.util.Vector;
  18. import java.util.Locale;
  19. import java.util.MissingResourceException;
  20. import java.util.ResourceBundle;
  21.  
  22. /**
  23.  * <p>Base class used to maintain a strongly typed enumeration.  This is
  24.  * the superclass of {@link AccessibleState} and {@link AccessibleRole}. 
  25.  * <p>The toDisplayString method allows you to obtain the localized string 
  26.  * for a locale independent key from a predefined ResourceBundle for the 
  27.  * keys defined in this class.  This localized string is intended to be
  28.  * readable by humans.
  29.  *
  30.  * @see AccessibleRole
  31.  * @see AccessibleState
  32.  *
  33.  * @version     1.9 08/26/98 21:14:01
  34.  * @author      Willie Walker
  35.  * @author    Peter Korn
  36.  */
  37. public abstract class AccessibleBundle {
  38.  
  39.     /**
  40.      * The locale independent name of the state.  This is a programmatic 
  41.      * name that is not intended to be read by humans.
  42.      * @see #toDisplayString
  43.      */
  44.     protected String key = null;
  45.  
  46.     /**
  47.      * Obtain the key as a localized string. 
  48.      * If a localized string cannot be found for the key, the 
  49.      * locale independent key stored in the role will be returned. 
  50.      * This method is intended to be used only by subclasses so that they 
  51.      * can specify their own resource bundles which contain localized
  52.      * strings for their keys.
  53.      * @param resourceBundleName the name of the resource bundle to use for 
  54.      * lookup
  55.      * @param locale the locale for which to obtain a localized string
  56.      * @return a localized String for the key.
  57.      */
  58.     protected String toDisplayString(String resourceBundleName, 
  59.                          Locale locale) {
  60.         // [[[FIXME:  WDW - obtaining resource bundles can be
  61.         // expensive, especially when obtaining them from ASCII
  62.         // properties files.  A time performace improvement can be
  63.         // made here if we cache the resource bundles by locale.
  64.         // We probably should also see if ResourceBundle itself
  65.         // caches these for us.  If it does, it would be nice.]]]
  66.         ResourceBundle resources;
  67.         String displayString = null;
  68.  
  69.         try {
  70.             resources = ResourceBundle.getBundle(resourceBundleName, 
  71.                                                  locale);
  72.             displayString = resources.getString(key);
  73.         } catch (MissingResourceException mre) {
  74.             System.err.println(mre 
  75.             + ":  " + resourceBundleName + " not found");
  76.         }
  77.  
  78.         if (displayString != null) {
  79.         return displayString;
  80.     } else {
  81.         return key;
  82.         }
  83.     }
  84.  
  85.     /**
  86.      * Obtain the key as a localized string. 
  87.      * If a localized string cannot be found for the key, the 
  88.      * locale independent key stored in the role will be returned. 
  89.      *
  90.      * @param locale the locale for which to obtain a localized string
  91.      * @return a localized String for the key.
  92.      */
  93.     public String toDisplayString(Locale locale) {
  94.         return toDisplayString(
  95.         "javax.accessibility.AccessibleResourceBundle", 
  96.                 locale);
  97.     }
  98.  
  99.     /** 
  100.      * Get localized string describing the key using the default locale.
  101.      * @return a localized String describing the key for the default locale
  102.      */
  103.     public String toDisplayString() {
  104.         return toDisplayString(Locale.getDefault());
  105.     }
  106.  
  107.     /** 
  108.      * Get localized string describing the key using the default locale.
  109.      * @return a localized String describing the key using the default locale
  110.      * @see #toDisplayString
  111.      */
  112.     public String toString() {
  113.         return toDisplayString();
  114.     }
  115. }
  116.